//---------------------------------------------------------------------------
// Author : Nguyen Duc Khanh
// Created Date : Thursday, January 10, 2008
//---------------------------------------------------------------------------


using System;
using System.Data;
using System.Data.SqlClient;
using vs.Core;

namespace Forum
{
    public class MemberController
    {
        #region Constructor
        public MemberController() { }
        #endregion

        public void Insert(MemberInfo mem)
        {
            DataTransaction trans = new DataTransaction();
            try
            {
                trans.BeginTransaction();
                Insert(mem, trans);
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
        }
        private const string INSERT_FORUM_MEMBER = "INSERT INTO FORUM_Member (UserName, Posts, Slogan, ImageUrl, Sign) "
         + " VALUES (@UserName, @Posts, @Slogan, @ImageUrl, @Sign)";
        public void Insert(MemberInfo mem, DataTransaction trans)
        {
            try
            {
                DataParameter[] param = new DataParameter[5];
                param[0] = new DataParameter("@UserName", mem.UserName);
                param[1] = new DataParameter("@Posts", mem.Posts);
                param[2] = new DataParameter("@Slogan", mem.Slogan);
                param[3] = new DataParameter("@ImageUrl", mem.ImageUrl);
                param[4] = new DataParameter("@Sign", mem.Sign);
                trans.Execute(INSERT_FORUM_MEMBER, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(MemberInfo mem)
        {
            DataTransaction trans = new DataTransaction();
            try
            {
                trans.BeginTransaction();
                Update(mem, trans);
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
        }
        private const string UPDATE_FORUM_MEMBER = "UPDATE FORUM_Member SET "
            + " Slogan = @Slogan, "
            + " ImageUrl = @ImageUrl, "
            + " Sign = @Sign "
            + " WHERE (UserName = @UserName)";
        public int Update(MemberInfo mem, DataTransaction trans)
        {
            int result = 0;
            try
            {
                DataParameter[] param = new DataParameter[4];
                param[0] = new DataParameter("@Slogan", mem.Slogan);
                param[1] = new DataParameter("@ImageUrl", mem.ImageUrl);
                param[2] = new DataParameter("@Sign", mem.Sign);
                param[3] = new DataParameter("@UserName", mem.UserName);
                result = trans.Execute(UPDATE_FORUM_MEMBER, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string DELETE_FORUM_MEMBER = "DELETE FROM FORUM_Member WHERE (UserName = @UserName)";
        public int Delete(string userName)
        {
            int result = 0;
            try
            {
                DataParameter param =  new DataParameter("@UserName", userName);
                result = DataFactory.Execute(DELETE_FORUM_MEMBER, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public MemberInfo Converting(DataRow drFORUM_Member)
        {
            MemberInfo result = null;
            try
            {
                result = new MemberInfo();
                result.UserName = (string)drFORUM_Member["UserName"];
                result.Posts = (int)drFORUM_Member["Posts"];
                result.Slogan = (string)drFORUM_Member["Slogan"];
                result.ImageUrl = (string)drFORUM_Member["ImageUrl"];
                result.Sign = (string)drFORUM_Member["Sign"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public MemberInfo Load(string userName)
        {
            MemberInfo result = null;
            DataTransaction trans = new DataTransaction();
            try
            {
                trans.BeginTransaction();
                result = Load(userName, trans);
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            return result;
        }
        private const string LOAD_FORUM_MEMBER = "SELECT * FROM FORUM_Member WHERE (UserName = @UserName)";
        public MemberInfo Load(string userName, DataTransaction trans)
        {
            MemberInfo result = null;
            try
            {
                DataParameter param = new DataParameter("@UserName", userName);
                DataTable dt = trans.Select(LOAD_FORUM_MEMBER, param);
                if (dt.Rows.Count > 0) result = Converting(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public bool Exist(string userName)
        {
            bool exist = false;
            DataTransaction trans = new DataTransaction();
            try
            {
                trans.BeginTransaction();
                exist = Exist(userName, trans);
                trans.Commit();
            }
            catch(Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            return exist;
        }

        public bool Exist(string userName, DataTransaction trans)
        {
            string strSQL = "select UserName from FORUM_Member where (UserName = @UserName) ";
            DataParameter p = new DataParameter("@UserName", userName);
            DataTable dt = trans.Select(strSQL, p);

            if(dt.Rows.Count > 0)
                return (dt.Rows[0][0].ToString() == userName);

            return false;
        }

        public void UpdatePostCount(string username, DataTransaction trans)
        {
            if (!Exist(username, trans))
            {
                MemberInfo mem = new MemberInfo();
                mem.UserName = username;
                mem.Posts = 1;
                Insert(mem, trans);
            }
            else
            {
                string strSQL = "UPDATE FORUM_Member SET "
                    + " Posts = (SELECT count(ReplyID) FROM FORUM_Reply WHERE UserName = @UserName)"
                    + " WHERE (UserName = @UserName)";
                DataParameter p = new DataParameter("@UserName", username);
                trans.Execute(strSQL, p);
            }
        }

        public static string jsImage(string clientID)
        {
            string js = @"
<script language=javascript>
    function popup2 (name,width,height) { 
         var options = ""toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1, width=""+width+"",height=""+height; 
         Cal2=window.open(name,""popup"",options); 
    }

    function SetImage(val)
    {
        var img = document.getElementById('" + clientID + @"');
        if(img) img.value = val;
    }
</script>
";
            return js;
        }

        public static bool EditMemberInfoRight(string dataUserName)
        {
            if (vs.Authentication.Authenticate.IsEditor) return true;
            if (vs.Authentication.Authenticate.UserName == dataUserName) return true;
            return false;
        }
    }
}
